home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / FileLogger.cpp < prev    next >
C/C++ Source or Header  |  2011-11-06  |  7KB  |  247 lines

  1. // FileZilla Server - a Windows ftp server
  2.  
  3. // Copyright (C) 2002-2004 - Tim Kosse <tim.kosse@gmx.de>
  4.  
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. #include "stdafx.h"
  20. #include "FileLogger.h"
  21. #include "Options.h"
  22.  
  23. //////////////////////////////////////////////////////////////////////
  24. // Konstruktion/Destruktion
  25. //////////////////////////////////////////////////////////////////////
  26.  
  27. CFileLogger::CFileLogger(COptions *pOptions)
  28. {
  29.     m_hLogFile = INVALID_HANDLE_VALUE;
  30.     m_pOptions = pOptions;
  31.  
  32.     m_pFileName = NULL;
  33.  
  34.     CheckLogFile();
  35. }
  36.  
  37. CFileLogger::~CFileLogger()
  38. {
  39.     if (m_hLogFile != INVALID_HANDLE_VALUE)
  40.         CloseHandle(m_hLogFile);
  41.  
  42.     delete [] m_pFileName;
  43. }
  44.  
  45. BOOL CFileLogger::Log(LPCTSTR msg)
  46. {
  47.     if (m_hLogFile==INVALID_HANDLE_VALUE)
  48.         return TRUE;
  49.  
  50.     char* utf8 = ConvToNetwork(msg);
  51.     if (!utf8)
  52.         return FALSE;
  53.  
  54.     DWORD numwritten;
  55.     if (!WriteFile(m_hLogFile, utf8, strlen(utf8), &numwritten, 0) || !WriteFile(m_hLogFile, "\r\n", 2, &numwritten, 0))
  56.     {
  57.         delete [] utf8;
  58.         CloseHandle(m_hLogFile);
  59.         m_hLogFile = INVALID_HANDLE_VALUE;
  60.         return FALSE;
  61.     }
  62.     delete [] utf8;
  63.  
  64.     return TRUE;
  65. }
  66.  
  67. BOOL CFileLogger::CheckLogFile()
  68. {
  69.     if (!m_pOptions->GetOptionVal(OPTION_ENABLELOGGING))
  70.     {
  71.         if (m_hLogFile != INVALID_HANDLE_VALUE)
  72.         {
  73.             CloseHandle(m_hLogFile);
  74.             m_hLogFile = INVALID_HANDLE_VALUE;
  75.         }
  76.         return TRUE;
  77.     }
  78.  
  79.     //Get logfile path
  80.     TCHAR path[MAX_PATH + 1000]; //Make it large enough
  81.     GetModuleFileName( 0, path, MAX_PATH );
  82.     LPTSTR pos=_tcsrchr(path, '\\');
  83.     if (pos)
  84.         *++pos=0;
  85.     _tcscat(path, _T("Logs\\"));
  86.     
  87.     //Get logfile name
  88.     _int64 nLogType = m_pOptions->GetOptionVal(OPTION_LOGTYPE);        
  89.     TCHAR filename[MAX_PATH + 1];
  90.     if (!nLogType)
  91.     {
  92.         _tcscpy(filename, _T("FileZilla Server.log"));
  93.     }
  94.     else
  95.     {
  96.         SYSTEMTIME time;
  97.         GetLocalTime(&time);
  98.         _stprintf(filename, _T("fzs-%d-%02d-%02d.log"), time.wYear, time.wMonth, time.wDay);
  99.     }
  100.  
  101.     if (m_hLogFile == INVALID_HANDLE_VALUE || (m_pFileName && _tcscmp(m_pFileName, filename)))
  102.     {
  103.         TCHAR buffer[MAX_PATH + 1000]; //Make it large enough
  104.         _tcscpy(buffer, path);
  105.         CreateDirectory(buffer, NULL);
  106.  
  107.         if (m_pFileName)
  108.             delete [] m_pFileName;
  109.         m_pFileName = new TCHAR[_tcslen(filename)+1];
  110.         _tcscpy(m_pFileName, filename);    
  111.         _tcscat(buffer, filename);
  112.         
  113.         if (m_hLogFile != INVALID_HANDLE_VALUE)
  114.             CloseHandle(m_hLogFile);
  115.         m_hLogFile = CreateFile(buffer, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, 0, OPEN_ALWAYS, 0, 0);
  116.         if (m_hLogFile == INVALID_HANDLE_VALUE)
  117.             return FALSE;
  118.         
  119.         SetFilePointer(m_hLogFile, 0, 0, FILE_END);
  120.     }
  121.     _int64 nLimit = m_pOptions->GetOptionVal(OPTION_LOGLIMITSIZE);
  122.     
  123.     if (nLogType)
  124.     {
  125.         //Different logfiles for each day
  126.         //Find all log files, delete old ones
  127.         //Also delete newer ones if total size exceeds limit
  128.  
  129.         //Get current date
  130.  
  131.         SYSTEMTIME time;
  132.         FILETIME curFileTime;
  133.         GetSystemTime(&time);
  134.         SystemTimeToFileTime(&time, &curFileTime);
  135.         _int64 nTime = curFileTime.dwLowDateTime + ((_int64)curFileTime.dwHighDateTime<<32);
  136.         
  137.         TCHAR buffer[MAX_PATH + 1000]; //Make it large enough
  138.         _tcscpy(buffer, path);
  139.         _tcscat(buffer, _T("fzs-*.log"));
  140.  
  141.         WIN32_FIND_DATA FindFileData;
  142.         WIN32_FIND_DATA NextFindFileData;
  143.         HANDLE hFind;
  144.         hFind = FindFirstFile(buffer, &NextFindFileData);
  145.         
  146.         _int64 nDeleteTime = (_int64)m_pOptions->GetOptionVal(OPTION_LOGDELETETIME);
  147.         if (nDeleteTime)
  148.             nDeleteTime = (nDeleteTime+1) * 60 * 60 * 24 * 10000000;
  149.  
  150.         //Count total size of all logs, delete the oldest log if exceeding limit
  151.         _int64 totalsize = 0;
  152.         CStdString oldestname;
  153.         _int64 oldestDate = 0;
  154.         
  155.         while (hFind != INVALID_HANDLE_VALUE)
  156.         {
  157.             FindFileData=NextFindFileData;
  158.             if (!FindNextFile(hFind, &NextFindFileData))
  159.             {
  160.                 FindClose(hFind);
  161.                 hFind = INVALID_HANDLE_VALUE;
  162.             }
  163.  
  164.             if (!_tcscmp(FindFileData.cFileName, _T(".")) || !_tcscmp(FindFileData.cFileName, _T("..")))
  165.                 continue;
  166.  
  167.             _int64 size = ((_int64)FindFileData.nFileSizeHigh<<32) + FindFileData.nFileSizeLow;
  168.             if (!_tcscmp(FindFileData.cFileName, m_pFileName))
  169.             {
  170.                 totalsize += size;
  171.                 continue;
  172.             }
  173.  
  174.             _int64 curtime=FindFileData.ftLastWriteTime.dwLowDateTime + ((_int64)FindFileData.ftLastWriteTime.dwHighDateTime<<32);
  175.             _int64 span = nTime - curtime;
  176.             TCHAR filename[MAX_PATH + 1000];
  177.             _tcscpy(filename, path);
  178.             _tcscat(filename, FindFileData.cFileName);
  179.             if (nDeleteTime && span > nDeleteTime)
  180.                 DeleteFile(filename); //File is too old, delete it
  181.             else
  182.             {
  183.                 totalsize += size;
  184.                 if (curtime < oldestDate || !oldestDate)
  185.                 {
  186.                     oldestDate = curtime;
  187.                     oldestname = filename;
  188.                 }
  189.             }
  190.         }
  191.         
  192.         if (_tcscmp(oldestname, _T("")) && nLimit && totalsize > nLimit*1024)
  193.         {
  194.             DeleteFile(oldestname);
  195.             return TRUE;
  196.         }
  197.     }
  198.  
  199.     //Single logfile, check size...
  200.     if (nLimit)
  201.     {
  202.         _int64 size = GetPosition64(m_hLogFile);
  203.         size /= 1024;
  204.         if (size > nLimit) //Log file too large, shrink it...
  205.         {
  206.             int curReadPos = (int)(size * 1024 - (nLimit * 1024) * 0.9); //New log size is 10% smaller than the set limit
  207.             int curWritePos =0;
  208.             const int bufsize = 16384; // 16KB
  209.             char buffer[bufsize];
  210.             DWORD numread;
  211.             DWORD numwritten;
  212.             BOOL bFirst = TRUE;;
  213.             do {
  214.                 SetFilePointer(m_hLogFile, curReadPos, 0, FILE_BEGIN);
  215.                 if (!ReadFile(m_hLogFile, buffer, bufsize, &numread, 0))
  216.                     break;
  217.                 curReadPos += numread;
  218.                 
  219.                 SetFilePointer(m_hLogFile, curWritePos, 0, FILE_BEGIN);
  220.                 if (bFirst) //Assure log starts with complete line
  221.                 {
  222.                     unsigned int i;
  223.                     for (i=0; i<numread; i++)
  224.                     {
  225.                         if (buffer[i] == '\n')
  226.                             break;
  227.                     }
  228.                     if (i >= (numread-1))
  229.                         continue;
  230.                     bFirst = FALSE;
  231.                     if (!WriteFile(m_hLogFile, buffer + i + 1, numread - i - 1, &numwritten, 0))
  232.                         break;
  233.                 }
  234.                 else
  235.                     if (!WriteFile(m_hLogFile, buffer, numread, &numwritten, 0))
  236.                         break;
  237.                     curWritePos += numwritten;
  238.                     
  239.             } while (numread == bufsize);
  240.             
  241.             SetFilePointer(m_hLogFile, curWritePos, 0, FILE_BEGIN);
  242.             SetEndOfFile(m_hLogFile);
  243.         }
  244.     }
  245.     return TRUE;
  246. }
  247.